home *** CD-ROM | disk | FTP | other *** search
AMOS Source Code | 1992-09-28 | 8.0 KB | 222 lines |
- '***************************
- '* AMOS Professional * INSTRUCTIONS COVERED
- '* *
- '* COLLISION DETECTION * Sprite Col
- '* * Bob Col
- '* (c) Europress Software * Spritebob Col
- '* * Bobsprite Col
- '* Ronnie Simpson * Col
- '*************************** Make mask
- '
- '-------------------------------------------
- 'DETECTING COLLISIONS
- '-------------------------------------------
- 'There are two stages involved in finding exactly which two objects have
- 'collided.
- '
- 'STAGE 1
- '
- 'First you test an individual sprite or bob using one of the four functions
- 'provided:-
- '
- '
- ' Sprite Col (test for a sprite -> sprite collision)
- ' Bob Col (test for a bob -> bob collision)
- ' Spritebob Col (test for a sprite -> bob collision)
- ' Bobsprite Col (test for a bob -> sprite collision)
- '
- 'Each of these functions use the same syntax and there is a normal and
- 'expanded form of each
- '
- ' normal- Sprite Col (1) Bobsprite Col(2)
- ' expanded- Sprite Col(1,5 To 7) Bobsprite Col(2,5 To 7)
- '
- 'Normal version tests if the named sprite or bob has collided with ANY object
- 'of the type tested.
- '
- 'Expanded version restricts the test to the stated series.
- '
- 'eg.
- ' C=Bob Col(4) (test if bob number 4 has collided with any other bobs)
- '
- ' C=Spritebob Col(2) (test if sprite 2 has collided with any bobs)
- '
- ' C=Sprite Col(1,2 To 7) (test if sprite 2 has collided with any of)
- ' ( the sprites numberd 2 -> 7 )
- '
- ' C=Bobsprite Col(1,3 To 5) (test if bob 1 has collided with any of)
- ' ( the sprites numberd 3,4 or 5 )
- '
- 'If a collision has occured then a value of -1 (or TRUE), otherwise the
- 'result will be 0 (or FALSE)
- '
- 'STAGE 2
- '
- 'To find exactly which object your tested bob or sprite has collided with
- 'the function =Col is used.
- '
- 'This function should be used as soon as possible after the original test to
- 'avoid any confusion.
- '
- 'It works in a similar manner as an array with each bob or sprite being
- 'associated with an element in this array according to its identification
- 'number.
- '
- 'If a collision has occured then a value of -1 (or TRUE) will be returned
- 'otherwise the result will be 0 (or FALSE).
- '
- 'If a negative number is used as the parameter for the col() function then
- 'an automatic test will be performed and the number returned will be the
- 'first bob or sprite which has collided, the only restriction with this is
- 'that sprite number 0 can not be tested with this version of the function.
- '
- 'Let's say for example you were writing a space invaders game with the
- 'objects numbered as follows:-
- '
- ' gun = Sprite 0
- ' shell = Sprite 7
- ' 48 aliens = bobs numbered 1-48
- '
- 'To check for collisions between the shell and the aliens then something
- 'like this could be used:-
- '
- ' If Spritebob Col(7,1 To 48) Then C=Col(-1):Proc RUBOUT[C]
- ' : :
- ' : :
- ' Procedure RUBOUT[N]
- ' Bob Off N : Boom
- ' End Proc
- '
- '-------------------------------------------
- 'FINAL NOTES
- '-------------------------------------------
- 'Collision detection routines rely on a sprite mask being present, if a call
- 'to No Mask has been made then collisions can not be detected.
- '
- 'If you are using Bob Col or Spritebob Col then the results by the =Col
- 'function will be bobs otherwise they will be sprites, therefore it makes
- 'sense to use the =Col function as soon as possible after the original test
- 'to avoid any confusion.
- '
- '-------------------------------------------
- 'WORKING EXAMPLE
- '-------------------------------------------
- Dim X(20),Y(20),DX(20),DY(20)
- Global X(),Y(),DX(),DY()
- Load "AMOSPro_Tutorial:Objects/Col.abk" : Make Mask
- Screen Open 0,640,200,4,Hires
- Flash Off : Curs Off : Palette $30,$5F5,$3 : Cls 0 : Pen 1 : Paper 2
- Reserve Zone 4
- Locate 0,5 : Centre Border$(Zone$(" Example 1 - Static bat with 1 ball ",1),4)
- Locate 0,10 : Centre Border$(Zone$(" Example 2- Moving bat with 1 ball + bricks ",2),4)
- Locate 0,15 : Centre Border$(Zone$(" Example 3- Moving bat with 8 balls ",3),4)
- Locate 0,20 : Centre Border$(Zone$(" Quit ",4),4)
- Do
- M=Mouse Zone
- If Mouse Key and M=4 Then Edit
- If Mouse Key and M>0
- On M Proc EXAMPLE1,EXAMPLE2,EXAMPLE3
- Sprite Off : Limit Mouse : Show On
- Repeat : Until Mouse Key=0
- End If
- Loop
- Procedure EXAMPLE1
- Screen Open 1,320,200,8,Lowres
- Get Sprite Palette : Colour 0,0 : Curs Off : Flash Off : Cls 0 : Hide On
- Set Paint 1 : Ink 5,7,4 : Bar 20,20 To 300,180 : Ink 7 : Box 21,21 To 299,179
- Bob 1,160,110,1
- Pen 7 : Paper 5 : Locate 0,20 : Centre "Press right mouse key to continue."
- Pen 2 : Locate 0,4 : Centre "Example 1"
- X=50 : Y=30 : DX=2 : DY=2
- Double Buffer
- '
- Do
- Exit If Mouse Key=2
- Add X,DX : Add Y,DY
- If X>294 or X<26 Then DX=-DX
- If Y>174 or Y<26 Then DY=-DY
- If Bob Col(1) Then Boom : DY=-DY : Add Y,DY*2
- Bob 2,X,Y,2
- Wait Vbl
- Loop
- '
- Screen Close 1
- End Proc
- Procedure EXAMPLE2
- Screen Open 1,320,200,8,Lowres : Double Buffer
- Get Sprite Palette : Colour 0,0 : Curs Off : Flash Off : Cls 0 : Hide On
- Set Paint 1 : Ink 5,7,4 : Bar 0,0 To 319,199 : Ink 7 : Box 1,1 To 318,198
- Ink 7 : Draw 1,199 To 318,199
- Pen 7 : Paper 5 : Locate 0,20 : Centre "Press right mouse key to continue"
- Pen 2 : Locate 0,6 : Centre "Example 2"
- X=40 : Y=40 : DX=2 : DY=2 : BALL=1 : COUNT=0
- Ink 5 : Locate 0,14 : Centre "Ball number"+Str$(BALL)
- Limit Mouse 175,150 To 445,150
- For N=1 To 9 : Bob N+2,N*32+16,30,3 : Next : X(12)=160 : Bob 12,160,100,5
- '
- Do
- Add X(12),3,-15 To 320 : Bob 12,X(12),,
- Exit If Mouse Key=2
- Bob 1,X Mouse-150,Y Mouse+40,1
- Add X,DX : Add Y,DY
- If X>312 or X<6 Then DX=-DX
- If Y<6 Then DY=-DY
- If Y>196 Then Boom : Y=8 : Inc BALL : Locate 24,14 : Print Str$(BALL)
- If Bob Col(2)
- C=Col(-1)
- If C=1
- Y=180 : DY=-DY : Shoot
- End If
- If C=12
- DY=-DY : Bell 1 : DX=Rnd(8)-4 : DY=-DY
- End If
- If C>2 and C<12
- Bob C,500,, : Inc COUNT : DY=-DY : Shoot
- If COUNT=9
- COUNT=0 : For N=1 To 9 : Bob N+2,N*32+16,30,3 : Next
- End If
- End If
- End If
- Bob 2,X,Y,2
- Wait Vbl
- '
- Loop
- Screen Close 1
- End Proc
- Procedure EXAMPLE3
- Screen Open 1,320,200,8,Lowres : Double Buffer
- Get Sprite Palette : Colour 0,0 : Curs Off : Flash Off : Cls 0 : Hide On
- Colour 17,0 : Colour 21,0 : Colour 25,0 : Colour 29,0
- Colour 18,$F : Colour 22,$F0 : Colour 26,$F00 : Colour 30,$FF0
- Colour 19,$F : Colour 23,$F0 : Colour 27,$F00 : Colour 31,$FF0
- Set Paint 1 : Ink 5,7,4 : Bar 0,0 To 319,199 : Ink 7 : Box 1,1 To 318,198
- Ink 7 : Draw 1,199 To 318,199 : Ink 2 : Bar 2,188 To 317,199
- Pen 7 : Paper 5 : Locate 0,20 : Centre "Press right mouse key to continue"
- Pen 2 : Locate 0,6 : Centre "Example 3"
- Pen 3 : Locate 7,15 : Print "Energy-"
- For N=0 To 7
- X(N)=Rnd(280)+160 : Y(N)=Rnd(160)+60
- Repeat
- DX(N)=Rnd(6)-3 : DY(N)=Rnd(6)-3
- Until DX(N)<>0 and DY(N)<>0
- Next
- NRG=80 : Autoback 1
- Ink 6 : Box 119,122 To 201,124 : Ink 1 : Draw 120,123 To NRG+120,123
- Limit Mouse 175,150 To 445,150 : Ink 0 : Timer=0
- '
- Do
- Exit If Mouse Key=2 or NRG<1
- Bob 1,X Mouse-150,Y Mouse+40,1
- For N=0 To 7
- Add X(N),DX(N) : Add Y(N),DY(N)
- Sprite N,X(N),Y(N),4
- If X(N)>442 or X(N)<134 Then DX(N)=-DX(N)
- If Y(N)<56 Then DY(N)=-DY(N)
- If Y(N)>234 Then Boom : Add NRG,-3 : Plot 121+NRG,123 : DY(N)=-DY(N)
- Next
- If Bobsprite Col(1,0 To 7) Then Bell 10 : C=Col(-1) : DY(C)=-DY(C) : Y(C)=230
- Loop
- '
- If NRG<1 Then Pen 6 : Locate 0,18 : Centre "GAME OVER - you lasted"+Str$(Timer/50)+" seconds" : Repeat : Until Mouse Key
- Screen Close 1
- End Proc